package com.itestor.iptv.modules.common.service.impl;


import java.sql.Timestamp;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import com.itestor.iptv.modules.common.bean.SystemLogQuery;
import com.itestor.iptv.modules.common.dao.SystemLogDao;
import com.itestor.iptv.modules.common.domain.SystemLog;
import com.itestor.iptv.modules.common.service.SystemLogService;


/**
 * 
 * @author Mr.li
 * 
 */
public class SystemLogServiceImpl implements SystemLogService {

    private static final Logger logger = Logger
            .getLogger(SystemLogServiceImpl.class);
    
    /**
     * 业务对象
     */
    private SystemLogDao systemLogDao;
    
    /**
     * 日志查询
     * @param systemLogQuery 
     */
    public void querySystemLogList(SystemLogQuery systemLogQuery) {
        StringBuilder QCountHQL = new StringBuilder();        
        QCountHQL.append("select count(t)from SystemLog t where 1=1 ");        
        
        StringBuilder hql = new StringBuilder();        
        hql.append("from SystemLog t where 1=1 ");        
        
        if (null != systemLogQuery.getLogType()) {
            QCountHQL.append(" and t.logType =:logType");
            hql.append(" and t.logType =:logType");
            
        }   
        if (null != systemLogQuery.getLogDesc()) {
            QCountHQL.append(" and t.logDesc like:logDesc");
            hql.append(" and t.logDesc like:logDesc");
        }   
        if (null != systemLogQuery.getStartTime()) {
            QCountHQL.append(" and t.logTime >:startTime");
            hql.append(" and t.logTime >:startTime");
            
        }   
        if (null != systemLogQuery.getEndTime()) {
            QCountHQL.append(" and t.logTime <:endTime");
            hql.append(" and t.logTime <:endTime");
        }           
          
        Query count_query = systemLogDao.getHibernateSession().createQuery(
                QCountHQL.toString()); 
        
        Query query = systemLogDao.getHibernateSession().createQuery(
                hql.toString()); 
        
        if (null != systemLogQuery.getLogType()) {
            count_query.setParameter("logType",systemLogQuery.getLogType() );
            query.setParameter("logType",systemLogQuery.getLogType() );
            
        }   
        if (null != systemLogQuery.getLogDesc()) {
            count_query.setParameter("logDesc","%"+systemLogQuery.getLogDesc()+"%" );
            query.setParameter("logDesc","%"+systemLogQuery.getLogDesc()+"%" );            
        }        
        if (null != systemLogQuery.getStartTime()) {
            count_query.setParameter("startTime",new Timestamp(systemLogQuery.getStartTime().getTime()) );
            query.setParameter("startTime",new Timestamp(systemLogQuery.getStartTime().getTime()) );
        }   
        if (null != systemLogQuery.getEndTime()) {
            count_query.setParameter("endTime",new Timestamp(systemLogQuery.getEndTime().getTime()) );            
            query.setParameter("endTime",new Timestamp(systemLogQuery.getEndTime().getTime()) );            
        }        
        if (systemLogQuery.getPagerCount() == -1) {
            Long count= (Long)count_query.uniqueResult();
            systemLogQuery.setPagerCount(count.intValue());
        }
        
        query.setFirstResult(systemLogQuery.getPagerOffset());
        query.setMaxResults(systemLogQuery.getPagerPerPage());
        
        systemLogQuery.setQueryResults(query.list());
    }
    
    
    /** 
     * @param pSystemLogDao
     */
    public void setSystemLogDao(SystemLogDao pSystemLogDao) {
        systemLogDao = pSystemLogDao;
    }

    public void create(SystemLog systemLog) {
        systemLogDao.create(systemLog);
    }
    
}
